package com.example.gulimall.product.api;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.example.common.constant.RedisConstant;
import com.example.gulimall.product.service.CategoryService;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.gulimall.product.entity.CategoryEntity;
import com.example.common.utils.PageUtils;
import com.example.common.utils.R;


/**
 * 商品三级分类
 *
 * @author gulin
 * @email gulinvip@gmail.com
 * @date 2021-05-28 22:29:41
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedissonClient redisson;
    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     * 查询所有树形分类
     */
    @RequestMapping("/list/tree")
    //@RequiresPermissions("product:category:list")
    public R listTree() {
        List<CategoryEntity> categoryEntityList = categoryService.queryCategoryTreeList();
        return R.ok().put("data", categoryEntityList);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:category:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category) {
        //设置读写锁并且移除缓存数据信息
        RReadWriteLock readWriteLock = redisson.getReadWriteLock(RedisConstant.CATEGORY_LOCK);
        RLock rLock = readWriteLock.writeLock();
        try {
            rLock.lock();
            categoryService.save(category);
            //删除缓存
            redisTemplate.delete(RedisConstant.CATEGORY_KEY);
        } catch (Exception e) {
           return R.error(e.getMessage());
        }finally {
           rLock.unlock();
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update/sort")
    //@RequiresPermissions("product:category:update")
    public R updateSort(@RequestBody CategoryEntity[] category) {
        //设置读写锁并且移除缓存数据信息
        RReadWriteLock readWriteLock = redisson.getReadWriteLock(RedisConstant.CATEGORY_LOCK);
        RLock rLock = readWriteLock.writeLock();
        try {
            rLock.lock();
            categoryService.updateBatchById(Arrays.asList(category));
            //删除缓存
            redisTemplate.delete(RedisConstant.CATEGORY_KEY);
        } catch (Exception e) {
            return R.error(e.getMessage());
        }finally {
            rLock.unlock();
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category) {
        categoryService.updateCascade(category);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds) {
        categoryService.removeByIds(Arrays.asList(catIds));
        //删除缓存
        redisTemplate.delete(RedisConstant.CATEGORY_KEY);
        return R.ok();
    }

}
